﻿using HY.Common;
using HY.Entity;
using HY.Service;
using HY.Web.Areas.Monitor.Models;
using HY.Web.Controllers;
using HY.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HY.Web.Areas.Monitor.Controllers
{
    public class MonitorDataController : BaseController
    {
        MonitorDataService service = new MonitorDataService();
        // GET: Monitor/MonitorData
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult List(int page, int limit)
        {
            ReturnModel returnModel = new ReturnModel();
            try
            {
                var tuple = service.GetPageList<V_MonitorData>("MonitorData MD LEFT JOIN Equipment E ON MD.EquipmentCode=E.EquipmentCode LEFT JOIN EquipmentBind EB ON E.ID=EB.EquipmentID LEFT JOIN Park ON EB.ParkID=Park.ID LEFT JOIN Storehouse S ON EB.StorehouseID=S.ID", "MD.ID,MD.EquipmentCode,MD.MonitorType,MD.MonitorValue,MD.AlarmStatus,MD.IsDeal,MD.CreateDate,Park.Code ParkCode,S.Code StorehouseCode", "", page, limit, "MD.CreateDate desc");
                returnModel = base.GetReturnModel(tuple.Item1, tuple.Item2);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                LogHelper.Error(new LogInfo() { UserID = UserInfo.UserID, LogType = LogType.Select, LogMessage = "实时监控数据" }, ex);
            }
            return Json(returnModel);
        }

        [HttpPost]
        public JsonResult Confirm(int id)
        {
            ReturnModel returnModel = new ReturnModel();
            LogInfo logInfo = new LogInfo() { UserID = base.UserInfo.UserID, LogType = LogType.Update };
            try
            {
                var model = service.GetById<MonitorData>(id);
                model.IsDeal = 1;
                //model.LastModifyUserID = base.UserInfo.UserID;
                //model.LastModifyDate = DateTime.Now;
                returnModel.count = service.Update(model);
                returnModel.msg = "确认成功";
                logInfo.LogMessage = $"监控数据{returnModel.msg}";
                LogHelper.Info(logInfo);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                returnModel.msg = "确认失败";
                logInfo.LogMessage = $"监控数据{returnModel.msg}";
                LogHelper.Error(logInfo, ex);
            }
            return Json(returnModel);
        }

        public ActionResult History()
        {
            return View();
        }

        [HttpPost]
        public JsonResult History(int page, int limit)
        {
            ReturnModel returnModel = new ReturnModel();
            try
            {
                var tuple = service.GetPageList<V_MonitorData>("MonitorData MD LEFT JOIN Equipment E ON MD.EquipmentCode=E.EquipmentCode LEFT JOIN EquipmentBind EB ON E.ID=EB.EquipmentID LEFT JOIN Park ON EB.ParkID=Park.ID LEFT JOIN Storehouse S ON EB.StorehouseID=S.ID", "MD.ID,MD.EquipmentCode,MD.MonitorType,MD.MonitorValue,MD.AlarmStatus,MD.IsDeal,MD.CreateDate,Park.Code ParkCode,S.Code StorehouseCode", "", page, limit, "MD.CreateDate desc");
                returnModel = base.GetReturnModel(tuple.Item1, tuple.Item2);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                LogHelper.Error(new LogInfo() { UserID = UserInfo.UserID, LogType = LogType.Select, LogMessage = "历史监控数据" }, ex);
            }
            return Json(returnModel);
        }

        public ActionResult Warn()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Warn(int page, int limit)
        {
            ReturnModel returnModel = new ReturnModel();
            try
            {
                var tuple = service.GetPageList<V_MonitorData>("MonitorData MD LEFT JOIN Equipment E ON MD.EquipmentCode=E.EquipmentCode LEFT JOIN EquipmentBind EB ON E.ID=EB.EquipmentID LEFT JOIN Park ON EB.ParkID=Park.ID LEFT JOIN Storehouse S ON EB.StorehouseID=S.ID", "MD.ID,MD.EquipmentCode,MD.MonitorType,MD.MonitorValue,MD.AlarmStatus,MD.IsDeal,MD.CreateDate,Park.Code ParkCode,S.Code StorehouseCode", "", page, limit, "MD.CreateDate desc");
                returnModel = base.GetReturnModel(tuple.Item1, tuple.Item2);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                LogHelper.Error(new LogInfo() { UserID = UserInfo.UserID, LogType = LogType.Select, LogMessage = "异常监控数据" }, ex);
            }
            return Json(returnModel);
        }
    }
}